package io.dcloud.uniplugin;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.bytedance.sdk.openadsdk.mediation.MediationConstant;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot;

public class NativePageActivity extends Activity {
    private TTRewardVideoAd mTTRewardVideoAd;
    private static final String TAG = "testAdMob";
    private boolean isRewardValid = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setBackgroundColor(Color.BLACK);
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(this);//需要传activity，切记！！！
        AdSlot adslot = new AdSlot.Builder()
                .setCodeId(getIntent().getStringExtra("codeId"))
                .setOrientation(TTAdConstant.VERTICAL)//横竖屏设置
                .setMediationAdSlot(new MediationAdSlot
                        .Builder()
                        .setExtraObject(MediationConstant.ADN_PANGLE, "pangleRewardCustomData")//服务端奖励验证透传参数
                        .setExtraObject(MediationConstant.ADN_GDT, "gdtRewardCustomData")
                        .setExtraObject(MediationConstant.ADN_BAIDU, "baiduRewardCustomData")
                        .build())
                .build();
        adNativeLoader.loadRewardVideoAd(adslot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int i, String s) {
            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                mTTRewardVideoAd = ttRewardVideoAd;
            }

            @Override
            public void onRewardVideoCached() {

            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                mTTRewardVideoAd = ttRewardVideoAd;
                //可在该回调后进行广告展示
                mTTRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
                    @Override
                    public void onAdShow() {
                        Log.d(TAG, "onAdShow: ");

                    }

                    @Override
                    public void onAdVideoBarClick() {
                        Log.d(TAG, "onAdVideoBarClick: ");
                    }

                    @Override
                    public void onAdClose() {
                        finishAction("onAdClose");
                    }

                    @Override
                    public void onVideoComplete() {
                        Log.d(TAG, "onVideoComplete: ");
                    }

                    @Override
                    public void onVideoError() {
                        finishAction("onVideoError");
                    }

                    @Override
                    public void onRewardVerify(boolean b, int i, String s, int i1, String s1) {

                    }

                    @Override
                    public void onRewardArrived(boolean isRewardValid, int code, Bundle bundle) {
                        ////奖励是否发放请依据isRewardValid
                        Log.d(TAG, "onRewardArrived:  isRewardValid " + isRewardValid + " code " + code);
                        finishAction("onRewardArrived");
                    }

                    @Override
                    public void onSkippedVideo() {
                        finishAction("onSkippedVideo");
                    }
                });
                mTTRewardVideoAd.showRewardVideoAd(NativePageActivity.this);
            }
        });
    }

    private void finishAction(String action) {
        Log.d(TAG, "finishAction: "+action);
        Intent intent = new Intent();
        intent.putExtra("action", action);
        intent.putExtra("isRewardValid", isRewardValid);
        intent.putExtra("code", 0);
        setResult(AdMobModule.REQUEST_CODE, intent);
        finish();
    }

    protected void onDestroy() {
        super.onDestroy();
        if (mTTRewardVideoAd != null && mTTRewardVideoAd.getMediationManager() != null) {
            mTTRewardVideoAd.getMediationManager().destroy();
        }
    }
}
